// globals
var swarm = new Object();
swarm.focus = false;
swarm.clearField = new Object();

// 
var swarmRender = function(objectsLayer, canvas, focusObj){
	// store
	swarm.canvas = canvas;
	swarm.objects = objectsLayer;
	if(focusObj){
		swarm.focus = focusObj;
		if(!swarm.metro) swarm.metro = setInterval("swarmFocus(9)", 35);
	}
	
	// prepare for canvas
	var ctx = canvas.getContext("2d");
	
	// recursively reposition swarm around focusObj
	// find peers of focusObj
	
	
	// render all connections
	ctx.strokeStyle = "rgba(0,0,0,6)";
	ctx.lineWidth = 0.5;
	ctx.beginPath();
	
	// do all connections
	var elements = objectsLayer.getElementsByTagName("*");
	for(var t=0; t<elements.length; t++){
		var node = elements[t];
		if(node.getAttribute("swarm")){
			if(obj(node.getAttribute("swarm"))) swarmConnectObjects(ctx, node, obj(node.getAttribute("swarm")));
		}
	}
	
	// render strokes
	ctx.clearRect(0, 0, canvas.offsetWidth, canvas.offsetHeight);
	ctx.stroke();
};

// connect 2 objects
var swarmConnectObjects = function(ctx, startObj, endObj){
	// calculate coordinates
	var startX = startObj.offsetLeft + Math.round(startObj.offsetWidth / 2);
	var startY = startObj.offsetTop + Math.round(startObj.offsetHeight / 2);
	var endX = endObj.offsetLeft + Math.round(endObj.offsetWidth / 2);
	var endY = endObj.offsetTop + Math.round(endObj.offsetHeight / 2);
	// draw on cancas
	ctx.moveTo(startX, startY);
	ctx.lineTo(endX, endY);
};

var swarmFocus = function(speed){
	var canvas = swarm.canvas;
	var objectsLayer = swarm.objects;
	var focusObj = swarm.focus;
	if(focusObj){
		var involved = new Array();
		var focusId = focusObj.getAttribute("id");
		if(focusObj.getAttribute("swarm")) involved.push(obj(focusObj.getAttribute("swarm")));
		var elements = objectsLayer.getElementsByTagName("*");
		for(var t=0; t<elements.length; t++){
			var node = elements[t];
			if(node.getAttribute("swarm")){
				if(node.getAttribute("swarm") == focusId) involved.push(node);
			}
		}
		// swarm all direct connected peers around focusObj
		//obj("bug").innerHTML += "<br/>" + involved[t].getAttribute("id");
		var radius = 80;
		var amountFinished = 0;
		for(var c=0; c<involved.length; c++){
			var degree = 360 / involved.length * c;
			//obj("bug").innerHTML += "<br/>" + t;
			var radian = degree / 180 * Math.PI;
			var cX = focusObj.offsetLeft + Math.cos(radian) * radius;
			var cY = focusObj.offsetTop - Math.sin(radian)*radius;
			// ease to the right coordinates
			involvedLeft = involved[c].offsetLeft + Math.round( (cX - involved[c].offsetLeft) / speed );
			involvedTop = involved[c].offsetTop + Math.round( (cY - involved[c].offsetTop) / speed );
			if(involved[c].offsetLeft == involvedLeft && involved[c].offsetTop == involvedTop) amountFinished++;
			involved[c].style.left = involvedLeft + "px";
			involved[c].style.top = involvedTop + "px";
		}
		if(amountFinished >= involved.length){
			clearInterval(swarm.metro);
			swarm.metro = false;
		}
		// redraw connections
		swarmRender(objectsLayer, canvas, false);
	}
};

var swarmInsert = function(objectsLayer, xmlDoc, xmlPath){
	var recursion = function(stepSrc, step, connection){
		for(var t=0; t<stepSrc.length; t++){
			var newId = "l" + step + "s" + t;
			objectsLayer.innerHTML += '<div id="' + newId + '" drag="' + stepSrc[t].getAttribute("drag") + '" class="swarmNode"><!----></div>';
			// insert xhtml content into this object
			if(connection) obj(newId).setAttribute("swarm", connection);
			obj(newId).innerHTML = xPath2s(stepSrc[t], "content");
			// recursion
			recursion(xPath(stepSrc[t], "s"), step + 1, "l" + step + "s" + t);
		}
	};
	// start
	var swarmSrc = xPath(xmlDoc, xmlPath)[0];
	recursion(xPath(swarmSrc, "s"), 0);
};
